package com.jeecms.wicool.dao.car.impl;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.car.UsedCarDao;
import com.jeecms.wicool.entity.car.UsedCar;

@Repository
public class UsedCarDaoImpl extends HibernateBaseDao<UsedCar, Integer>
		implements UsedCarDao {

	@Override
	protected Class<UsedCar> getEntityClass() {
		// TODO Auto-generated method stub
		return UsedCar.class;
	}
	@Override
	public Pagination getPage(String title,Integer carBrandId,Integer carSeriesId,Integer carTypeId,int pageNo, int pageSize) {
			Finder f = Finder.create("select bean from UsedCar bean where 1=1 ");
			if (!StringUtils.isBlank(title)){
				f.append(" and bean.title like :title");
				f.setParam("title", "%" + title + "%");
			}if (null!=carBrandId){
				f.append(" and bean.carBrand.id = :carBrandId");
				f.setParam("carBrandId",carBrandId);
			}if (null!=carSeriesId){
				f.append(" and bean.carSeries.id = :carSeriesId");
				f.setParam("carSeriesId",carSeriesId);
			}if (null!=carTypeId){
				f.append(" and bean.carType.id = :carTypeId");
				f.setParam("carTypeId",carTypeId);
			}
			f.append(" order by bean.id desc");
			return find(f, pageNo, pageSize);
	}
	@Override
	public UsedCar findById(Integer id) {
		UsedCar entity = get(id);
		return entity;
	}
	@Override
	public List<UsedCar> findByCarTypeId(Integer carTypeId){
		Finder f = Finder.create("select bean from UsedCar bean where 1=1 ");
		f.append(" and bean.carType.id = :carTypeId");
		f.setParam("carTypeId",carTypeId);
		return find(f);
	}
	@Override
	public UsedCar save(UsedCar bean) {
		getSession().save(bean);
		return bean;
	}
	@Override
	public UsedCar deleteById(Integer id) {
		UsedCar entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
			getSession().flush();
		}
		return entity;
	}
}